package com.aj.activity.fragment;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.aj.activity.R;
import com.aj.activity.adapter.PrayLetterAdapter;
import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.FamilySpace;
import com.aj.databases.entry.PrayLetterInfo;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.AddPrayLetterRequest;
import com.aj.http.request.BabyPrayLetterRequest;
import com.aj.http.response.BabyPrayLetterResponse;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.utils.Contants;

public class PrayLetterFragment extends Fragment {
	private ListView listView;
	private FamilySpace familySpace;
	private PrayLetterAdapter prayLetterAdapter;
	private EditText enter_pray_edittext;
	private Button sendbutton;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_prayletter, container,
				false);
		initView(view);
		return view;
	}

	private void resume() {
		UserDao userDao = new UserDao(getActivity());
		new BabyPrayLetterRequest().excute(getActivity(), userDao
				.getCurrentUser(LoveHomeApplication.token).getUser_id(),
				familySpace.getId(), 50, 0, new IHttpCallBack() {
					@Override
					public void success(String result) {
						try {
							BabyPrayLetterResponse response = new BabyPrayLetterResponse(
									new JSONObject(result));
							if (response.getCommon() != null
									&& response.getCommon().getReturnCode() != null
									&& response
											.getCommon()
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
								List<PrayLetterInfo> prayLetterInfoList = response
										.getBabyPrayLetterInfo()
										.getPrayLetterInfo();
								if (prayLetterInfoList != null
										&& prayLetterInfoList.size() > 0) {
									prayLetterAdapter = new PrayLetterAdapter(
											getActivity(), prayLetterInfoList);
									listView.setAdapter(prayLetterAdapter);
								}
							} else {
								Toast.makeText(getActivity(),
										response.getCommon().getErrorMsg(),
										Toast.LENGTH_LONG).show();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void fail(String result) {
						try {
							Common common = new Common(new JSONObject(result));
							Toast.makeText(getActivity(), common.getErrorMsg(),
									Toast.LENGTH_LONG).show();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	private void initView(View view) {
		listView = (ListView) view.findViewById(R.id.list);
		enter_pray_edittext = (EditText) view
				.findViewById(R.id.enter_pray_edittext);
		sendbutton = (Button) view.findViewById(R.id.sendbutton);
		sendbutton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (enter_pray_edittext.getText().toString().trim().length() == 0) {
					Toast.makeText(getActivity(), "请输入祈愿信内容~",
							Toast.LENGTH_LONG).show();
					return;
				}
				UserDao userDao = new UserDao(getActivity());
				new AddPrayLetterRequest().excute(getActivity(),
						userDao.getCurrentUser(LoveHomeApplication.token)
								.getUser_id(), familySpace.getId(), null, null,
						enter_pray_edittext.getText().toString(),
						new IHttpCallBack() {
							@Override
							public void success(String result) {
								try {
									Common common = new Common(new JSONObject(
											result));
									Toast.makeText(getActivity(),
											common.getErrorMsg(),
											Toast.LENGTH_LONG).show();
									if (common.getReturnCode() != null
											&& common
													.getReturnCode()
													.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
										resume();
										enter_pray_edittext.setText("");
									}
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}

							@Override
							public void fail(String result) {
								try {
									Common common = new Common(new JSONObject(
											result));
									Toast.makeText(getActivity(),
											common.getErrorMsg(),
											Toast.LENGTH_LONG).show();
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}
						});
			}
		});
		familySpace = (FamilySpace) getActivity().getIntent()
				.getSerializableExtra(Contants.USERINFOFLAG_STRING);
		resume();
	}
}
